//
//  PointDetailVC.m
//  xychain_DM_iOS
//
//  Created by Jpwang on 2018/6/11.
//  Copyright © 2018年 Jpwang. All rights reserved.
//

#import "PointDetailVC.h"
#import "PointItemCell.h"
#import "PointInfo.h"

@interface PointDetailVC () <UITableViewDelegate, UITableViewDataSource> {
    NSInteger mStart;
}

@property(weak, nonatomic)IBOutlet UIButton *backBt;
@property(weak, nonatomic)IBOutlet UILabel *profitLb;
@property(weak, nonatomic)IBOutlet UITableView *tableView;
@property(strong,nonatomic)NSMutableArray *dataSource;

@end

@implementation PointDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initView];
    [self requestYesterdayProfit];
    [self requestData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initView {
    self.dataSource = [[NSMutableArray alloc] init];
    [[self.backBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"PointItemCell" bundle:nil] forCellReuseIdentifier:@"PointItemCellID"];
    self.tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        self->mStart = 0;
        [self requestData];
    }];
    self.tableView.mj_footer = [MJRefreshFooter footerWithRefreshingBlock:^{
        self->mStart = self.dataSource.count;
        [self requestData];
    }];
}

- (void)requestYesterdayProfit {
    //设备昨日收益
    [[[HttpRequest shareInstance] signGet:w_yesterday_point map:nil] subscribeNext:^(id x) {
        if ([x[@"code"] intValue] == 200) {
            @try {
                id data = x[@"data"];
                double profit = data ? [data doubleValue] : 0.0;
                NSString *text = [[NSNumber numberWithDouble:profit] stringValue];
                self.profitLb.attributedText = [self handleBigSmallText:text];
            } @catch (NSException *exception) {
                self.profitLb.text = @"0";
            }
        } else {
            self.profitLb.text = @"0";
        }
    } error:^(NSError *error) {
        self.profitLb.text = @"0";
    }];
}

- (void)requestData {
    [self showLoading:@"加载中"];
    NSDictionary *dic = @{@"start": [NSNumber numberWithInteger:mStart], @"length": [NSNumber numberWithInt:mLength]};
    [[[HttpRequest shareInstance] signGet:w_point_detail map:[NSMutableDictionary dictionaryWithDictionary:dic]] subscribeNext:^(id x) {
        [self hideLoading];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([x[@"code"] intValue] == 200) {
            NSError *error;
            NSArray *temp = [MTLJSONAdapter modelsOfClass:[PointInfo class] fromJSONArray:x[@"data"] error:&error];
            if (!error) {
                if (self->mStart == 0) {
                    [self.dataSource removeAllObjects];
                }
                if (self.dataSource.count<mLength) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }
                [self.dataSource addObjectsFromArray:temp];
                [self.tableView reloadData];
            } else {
                [JPToast showText:@"数据解析错误"];
            }
        } else {
            [JPToast showText:x[@"message"]];
        }
    } error:^(NSError *error) {
        [self hideLoading];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [JPToast showText:@"网络错误"];
    }];
}

#pragma mark UITableView delegate and datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (0<=indexPath.row && indexPath.row<self.dataSource.count) {
        return 100;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PointItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PointItemCellID"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setBlock:^(PointInfo *model) {
        NSString *msg = [NSString stringWithFormat:@"\n\n收益已转入绑定钱包\n\n%@\n\n", model.address];
        UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"" message:msg preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [vc addAction:cancel];
        [self presentViewController:vc animated:YES completion:nil];
    }];
    [cell performSelector:@selector(setModel:) withObject:self.dataSource[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (0<=indexPath.row && indexPath.row<self.dataSource.count) {
        PointInfo *model = self.dataSource[indexPath.row];
        NSString *msg = [NSString stringWithFormat:@"\n\n收益已转入绑定钱包\n\n%@\n\n", model.address];
        UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"" message:msg preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [vc addAction:cancel];
        [self presentViewController:vc animated:YES completion:nil];
    }
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end
